let Menu = require('Menu')
let UserDataManager = require('UserDataManager')
let WX = require('WX')

let GameWinBoard = cc.Class({
  extends: cc.Component,

  properties: {
    moneyTxt: cc.Label,
    nextTxt: cc.Label,
    noThank: cc.Node,
    closeBtn: cc.Node
  },

  statics: {
    instance: null
  },

  ctor () {
    if (!GameWinBoard.instance) {
      GameWinBoard.instance = this
      this.chapterExp = 0
    }
  },

  onLoad () {},

  start () {},

  open (args) {
    /*
      params 
        args {chapterExp} 
    */
    WX.showBannerAd(() => {
      this.noThank.y += 100
    })
    UserDataManager.saveUserData2(
      'dollar',
      UserDataManager.getUserData().dollar + args.chapterExp
    )

    this.chapterExp = args.chapterExp
    this.node.opacity = 0
    this.node.active = true
    this.node.runAction(cc.fadeTo(1.5, 255))

    this.nextTxt.string = '双倍奖励'
    if (!WX.shareEnable) {
      this.noThank.active = false
      this.closeBtn.active = true
    } else {
      this.closeBtn.active = false
      this.noThank.active = true
    }
    this.updateMoneyTxt()
  },

  updateMoneyTxt () {
    this.moneyTxt.string = this.chapterExp
  },

  close(){
    this.toMenu();
  },

  toMenu () {
    console.log('GameWinBoard-toMenu')
    this.noThank.y -= 100
    this.node.active = false
    let _Main = this.node.parent.getComponent('Main')
    _Main.clearAllSceneObjects()
    _Main.clearAllSpecial()
    _Main.updateBoard()
    Menu.instance.open()
  },

  doubleReward () {
    console.log('GameWinBoard-doubleReward')
    let nextChapter = () => {
      UserDataManager.saveUserData2(
        'dollar',
        UserDataManager.getUserData().dollar + this.chapterExp
      )
      this.toMenu()
    }

    WX.watchVideoAD(() => {
      this.updateMoneyTxt()
      nextChapter()
    })
  },

  update (dt) {}
})
